package hn.cch.grammar.java;


import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * 系统工具类
 */
public class SystemDemo {


    /**
     * 是否是windows系统
     *
     * @return
     */
    public static boolean isWindows() {
        return System.getProperty("os.name").matches("^(?i)Windows.*$");
    }


    /**
     * 是否是linux系统
     *
     * @return
     */
    public static boolean isLinux() {
        return System.getProperty("os.name").matches("^(?i)Linux.*$");
    }


    /**
     * 获取主机IP
     *
     * @return
     */
    public static String ipHost() throws Exception {

        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            String hostAddress = inetAddress.getHostAddress();
            // logger.info("HostAddress:{}", hostAddress);
            return hostAddress;
        } catch (UnknownHostException e) {
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 获取所有IP
     *
     * @param flag true:IPv4; false:IPv6
     * @return
     */
    public static List<String> ipList(boolean flag) throws SocketException {

        List<String> ips = new ArrayList<>();

        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                if (flag) {
                    if (inetAddress instanceof Inet4Address) {// IPv4
                        String hostAddress = inetAddress.getHostAddress();
                        ips.add(hostAddress);
                    }
                } else {
                    if (inetAddress instanceof Inet6Address) {// IPv6
                        String hostAddress = inetAddress.getHostAddress();
                        ips.add(hostAddress);
                    }
                }
            }
        }
        return ips;

    }

    public static void getProperty() {
        System.out.println("java.version:" + System.getProperty("java.version"));
        System.out.println("java.class.version:" + System.getProperty("java.class.version"));

        System.out.println("java.vendor:" + System.getProperty("java.vendor"));
        System.out.println("java.vendor.url:" + System.getProperty("java.vendor.url"));

        System.out.println("java.home:" + System.getProperty("java.home"));
        System.out.println("java.compiler:" + System.getProperty("java.compiler"));

        System.out.println("java.class.path：" + System.getProperty("java.class.path"));
        System.out.println("java.library.path:" + System.getProperty("java.library.path"));
        System.out.println("java.io.tmpdir:" + System.getProperty("java.io.tmpdir"));
        System.out.println("java.ext.dirs:" + System.getProperty("java.ext.dirs"));

        System.out.println("os.name:" + System.getProperty("os.name"));
        System.out.println("os.arch:" + System.getProperty("os.arch"));
        System.out.println("os.version:" + System.getProperty("os.version"));

        System.out.println("file.separator:" + System.getProperty("file.separator"));
        System.out.println("path.separator:" + System.getProperty("path.separator"));
        System.out.println("line.separator:" + System.getProperty("line.separator"));

        System.out.println("user.name:" + System.getProperty("user.name"));
        System.out.println("user.home:" + System.getProperty("user.home"));
        System.out.println("user.dir:" + System.getProperty("user.dir"));
    }

}
